import { createRouter, createWebHistory } from 'vue-router'
import AttractionView from '@/views/AttractionView.vue'
import IndexView from '@/views/IndexView.vue'
import OrderView from '@/views/OrderView.vue'
import RefundView from '@/views/RefundView.vue'
import TicketView from '@/views/TicketView.vue'
import PersonInfoView from '@/views/PersonInfoView.vue'
import { useLoginStore } from "@/stores/LoginStore"
import { ElMessage } from 'element-plus'
import { fa } from 'element-plus/es/locales.mjs'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/',
      name:'indexView',
      component:IndexView
    },
    {
      path:'/attraction',
      name:'attractionView',
      component:AttractionView,
      meta:{title:'景点'},
     
    },
     
    { path:'/ticket',
      name:'ticketView',
      component:TicketView,
     
    },
    
    {
          path:'/order',
          name:'orderView',
          component:OrderView,
          meta:{title:'订单',auth:true}
    },
    {
      path:'/person',
      name:'personView',
      component:PersonInfoView,
      meta:{title:'个人信息',auth:true},
      children:[
        
        {
          path:'refund',
          name:'refundView',
          component:RefundView,
          meta:{title:'退款信息'}
        }
            ]
      }
    
  ],
})

router.beforeEach((to,from)=>{
  let loginStore=useLoginStore()
  if(to.meta.auth){
    if(loginStore.loginToken){
      return true;
    }else{
      ElMessage.error({
        message:'sorry,请先登录',
        type:'error'
      })
      return false;
    }
  }
  return true;
})

router.afterEach((to,from)=>{
  console.log('全局路由守卫-后置')
  //直接设置网页的title
  document.title = (to.meta.title||'欢迎') as string;
  return true;
})

export default router
